package com.kun.admin.authority.controller;

import com.kun.admin.authority.model.Department;
import com.kun.admin.authority.service.DepartmentService;
import com.kun.base.web.model.AjaxResult;
import com.xiaoleilu.hutool.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author likun
 * 2018/5/22 11:13
 **/
@Controller
@RequestMapping("/admin/department")
@Api(description = "部门管理接口")
public class AdminDepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ResponseBody
    @RequestMapping("/findAllDepartmentTree")
    @ApiOperation(value = "获取所有部门的树形菜单", response = Department.class)
    public Department findAllDepartmentTree() {
        return departmentService.findAllDepartmentTree();
    }

    @ResponseBody
    @RequestMapping("/addDepartment")
    @ApiOperation(value = "添加部门", response = AjaxResult.class)
    public AjaxResult addDepartment(@ApiParam(name = "部门名称") String name,
                                    @ApiParam(name = "父节点id") Long parentId) {
        if (StrUtil.isNotEmpty(name) && parentId != null) {
            return new AjaxResult(departmentService.addDepartment(name, parentId));
        } else {
            return new AjaxResult(false, "参数不能为空");
        }
    }

    @ResponseBody
    @RequestMapping("/editDepartment")
    @ApiOperation(value = "修改部门信息",response = AjaxResult.class)
    public AjaxResult editDepartment(@ApiParam("部门id") Long id, @ApiParam("部门名称") String name){
        if(id!=null && StrUtil.isNotEmpty(name)){
            return new AjaxResult(departmentService.editDepartment(id,name));
        }
        return new AjaxResult(false);
    }

    @ResponseBody
    @RequestMapping("/deleteDepartment")
    @ApiOperation(value = "删除部门")
    public AjaxResult deleteDepartment(Long id){
        if (id!=null){
            return new AjaxResult(departmentService.deleteDepartment(id));
        }
        return new AjaxResult(false);
    }
}
